package com.service.impl;

import com.entity.DinerOrder;
import com.entity.OrderSummary;
import com.mapper.DinerOrderDetallMapper;
import com.mapper.DinerOrderMapper;
import com.service.DinerOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Component
public class DinerOrderServiceImpl implements DinerOrderService {
    @Autowired
    DinerOrderMapper dinerOrderMapper;
    @Autowired
    DinerOrderDetallMapper dinerOrderDetallMapper;


    public static String randomOrderCode() {
        SimpleDateFormat dmDate = new SimpleDateFormat("yyyyMMddHHmmss");
        String randata = getRandom(6);
        Date date = new Date();
        String dateran = dmDate.format(date);
        String Xsode = dateran + randata;
        if (Xsode.length() < 24) {
            Xsode = Xsode + 0;
        }
        return Xsode;
    }

    public static String getRandom(int len) {
        Random r = new Random();
        StringBuilder rs = new StringBuilder();
        for (int i = 0; i < len; i++) {
            rs.append(r.nextInt(10));
        }
        return rs.toString();
    }

    @Override
    public List<DinerOrder> getDinerOrderInfo(Integer id) {
        return dinerOrderMapper.getDinerOrderInfo(id);
    }

    @Override
    public List<DinerOrder> getDinerOrderInfoStatus() {
        return dinerOrderMapper.getDinerOrderInfoStatus();
    }

    /**
     * 添加订单信息
     */
    @Override
    @Transactional
    public int insertDinrORderInfo(DinerOrder dinerOrder) {
        dinerOrder.setSerialNo(randomOrderCode());
        int i = dinerOrderMapper.insertDinrORderInfo(dinerOrder);
        for (int j = 0; j < dinerOrder.getDinerOrderDetailList().size(); j++) {
            dinerOrder.getDinerOrderDetailList().get(j).setOrderId(dinerOrder.getId());
        }
        i = dinerOrderDetallMapper.insertOrderDetallInfo(dinerOrder.getDinerOrderDetailList());
        return i;
    }

    @Override
    public int UpdateDinerrOrderState(DinerOrder dinerOrder) {
        SimpleDateFormat dmDate = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        String dateran = dmDate.format(date);
        switch (dinerOrder.getStatus()) {
            case 1:
                //支付时间
                dinerOrder.setPayTime(dateran);
                break;
            case 2:
                //取消时间
                dinerOrder.setCancelTime(dateran);
                break;
            case 5:
                //完成时间
                dinerOrder.setFinishTime(dateran);
            case 0:
                dinerOrder.setFinishTime(null);
                break;
        }
        return dinerOrderMapper.UpdateDinerrOrderState(dinerOrder);
    }

    @Override
    public List<DinerOrder> getDinerOrderThisInfo(Integer id, Integer orderId) {
        return dinerOrderMapper.getDinerOrderThisInfo(id,orderId);
    }

    @Override
    public List<DinerOrder> getDinerOrderpayInfo() {
        return dinerOrderMapper.getDinerOrderpayInfo();
    }

    @Override
    public List<DinerOrder> getDinerOrderHistoryInfo(Integer flag,String createBy) {
        return dinerOrderMapper.getDinerOrderHistoryInfo(flag,createBy);
    }

    @Override
    public List<DinerOrder> getDinerORderInfo(Integer orderId) {
        return dinerOrderMapper.getDinerORderInfo(orderId);
    }

    @Override
    public List<OrderSummary> getOrderSummary(String goods_name, String create_time) {
        return dinerOrderMapper.getOrderSummary(goods_name,create_time);
    }
}
